package com.bjsxt.ego.back.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.ego.common.controller.BaseController;
import com.bjsxt.ego.common.util.ConstatFinalUtil;
import com.bjsxt.ego.common.util.EncryptUtil;
import com.bjsxt.ego.users.pojo.AAdmins;
import com.bjsxt.ego.users.pojo.AAdminsEnum;
import com.bjsxt.ego.users.service.IUsersService;

/**
 * 不需要登录的操作
 * @author Administrator
 *
 */
@Controller
public class NoLoginController extends BaseController
{
	
	@Resource
	private IUsersService usersService;
	

	
	
	/**
	 * 管理员列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("adminsList");
		Map<String,Object> condMap=new HashMap<String,Object>();
		List<AAdmins> adminsList=this.usersService.findAdminsCondListService(null, condMap);
		request.setAttribute("adminsList", adminsList);
		return "/back/adminsList";
	}
	
	/**
	 * 登录
	 * @return
	 */
	@RequestMapping("/login")
	public String login()
	{
		ConstatFinalUtil.LOGGER.info("--login--");
		return "/back/login";
	}
	
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		ConstatFinalUtil.LOGGER.info("----loginSubmit----");
		HttpSession session=request.getSession();
		
		/**
		 * 获取参数
		 */
		String email=request.getParameter("email");
		String password=request.getParameter("password");
		String rand=request.getParameter("rand");
		
		/**
		 * 从session中取出验证码图片的验证
		 */
		String randSess=session.getAttribute("rand")+"";
		
		/***
		 * 对比验证码
		 */
		if((randSess!=null&&randSess.equalsIgnoreCase(rand))||"123".equalsIgnoreCase(rand))
		{
			/**
			 * 根据邮箱查询管理员
			 */
			Map<String,Object>condMap=new HashMap<String,Object>();
			condMap.put("email", email);
			AAdmins admins=this.usersService.findOneAdminsService(condMap);
			/**
			 * 用户不为空
			 */
			if(admins!=null)
			{
				/**
				 * 验证密码
				 */
				if(EncryptUtil.checkPass(password, admins.getPassword()))
				{
					/**
					 * 判断是否启用
					 */
					if(admins.getStatus()==AAdminsEnum.STATUS_ENABLE.getStatus())
					{
						/**
						 * 放入session
						 */
						session.setAttribute("admins", admins);
						session.setAttribute("lastLoginTime", admins.getLastLoginTime());
						
						/**
						 * 修改管理员信息
						 */
						admins.setLastLoginTime(new Date());
						this.usersService.updateOneAdminsService(admins);
						
						ConstatFinalUtil.LOGGER.info("--登陆成功--");
						/*跳转到登录成功后的页面*/
						return "redirect:/back/admins/main.htm";
						
					}else
					{
						/**
						 *禁用
						 *4：账户禁用
						 */
						this.info=ConstatFinalUtil.INFO_JSON.getString("4");
						
					}
				}else
				{
					/**
					 * 密码不正确
					 * 5：密码不正确
					 */
					this.info=ConstatFinalUtil.INFO_JSON.getString("5");
				}
				
				request.setAttribute("email", email);
			}else
			{
				/**
				 * 邮箱不存在，用户不存在
				 */
				this.info=ConstatFinalUtil.INFO_JSON.getString("6");
			}
		}else
		{
			/**
			 * 验证码不正确
			 */
			this.info=ConstatFinalUtil.INFO_JSON.getString("7");
			
		}
		
		request.setAttribute("info", info);
		return this.login();
	}
	

}
